package io.rong.app.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;

import com.yunshipei.enterplorer.R;
import com.yunshipei.model.UserInfo;
import com.yunshipei.receiver.PushNotificationReceiver;

import io.rong.imkit.RongContext;

/**
 * Created by cike on 15-11-15.
 * IM推送消息状态栏通知
 */
public class CommonNotification {
    private CommonNotification commonNotification = null;
    private NotificationManager nm = null;
    private Notification notification = null;
    private String contentTitle = "办公浏览器";       //提醒标题
    private String contentStr = "";                 //提醒内容
    private Intent intent = null;                   //提醒点击事件

    public CommonNotification(Intent intent, String contentTitle, String contentStr, String url, UserInfo userInfo, boolean shouldSound, int num) {

        if (contentTitle != null)
            this.contentStr = contentStr;
        if (contentStr != null)
            this.contentTitle = contentTitle;
        PendingIntent pendingIntent = PendingIntent.getActivity(RongContext.getInstance(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //如果消息的内容包含该参数，说明是推送的消息，直接打开
        if (userInfo != null) {
            String isAdmin = userInfo.getIsAdmin();
            if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(isAdmin) && "true".equals(isAdmin)) {
                Intent castIntent = new Intent(RongContext.getInstance(), PushNotificationReceiver.class);
                castIntent.putExtra(PushNotificationReceiver.PUSH_URL, url);
                pendingIntent = PendingIntent.getBroadcast(RongContext.getInstance(), 0, castIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
        }

        Notification.Builder builder = new Notification.Builder(RongContext.getInstance())
                //.setLargeIcon(getAppIcon())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(this.contentTitle)
                .setContentText(this.contentStr)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        if (shouldSound) {
            builder.setDefaults(Notification.DEFAULT_SOUND);
        }

        notification = builder.build();

        nm = (NotificationManager) RongContext.getInstance().getSystemService(RongContext.getInstance().NOTIFICATION_SERVICE);
    }

    public void showNotification() {
        nm.notify(0, notification);
    }

    public String getContentTitle() {
        return contentTitle;
    }

    public void setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
    }

    public String getContentStr() {
        return contentStr;
    }

    public void setContentStr(String contentStr) {
        this.contentStr = contentStr;
    }

    public Intent getIntent() {
        return intent;
    }

    public void setIntent(Intent intent) {
        this.intent = intent;
    }

    private Bitmap getAppIcon() {
        BitmapDrawable bitmapDrawable;
        Bitmap appIcon;
        bitmapDrawable = (BitmapDrawable) RongContext.getInstance().getApplicationInfo().loadIcon(RongContext.getInstance().getPackageManager());
        appIcon = bitmapDrawable.getBitmap();
        return appIcon;
    }
}
